﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class MenuConfig: IMenuConfig
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public MenuConfig(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.MenuConfig GetMenuConfig(System.Int32 menuID)
		{
			BEGDC.PreContract.DAL.MenuConfig menuConfig = BEGDC.PreContract.DAL.MenuConfig.TryFind(menuID);

            return menuConfig;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetMenuConfig_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM MenuConfig";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteMenuConfig(System.Int32 menuID)
		{
			try
            {
				BEGDC.PreContract.DAL.MenuConfig menuConfig = BEGDC.PreContract.DAL.MenuConfig.TryFind(menuID);
                menuConfig.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("MenuConfig", string.Format("Error occured when deleting the MenuConfig, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddMenuConfig(System.String menuTitle, System.String menuTitle_CN, System.String menuURL, Nullables.NullableInt32 parentMenuID, Nullables.NullableInt32 menuSequenceID, System.Boolean isEnabled, System.String target, System.String menuGroup)	
		{
			try
            {
				BEGDC.PreContract.DAL.MenuConfig menuConfig = new BEGDC.PreContract.DAL.MenuConfig();
				
				menuConfig.MenuTitle = menuTitle;
				menuConfig.MenuTitle_CN = menuTitle_CN;
				menuConfig.MenuURL = menuURL;
				menuConfig.ParentMenuID = parentMenuID;
				menuConfig.MenuSequenceID = menuSequenceID;
				menuConfig.IsEnabled = isEnabled;
				menuConfig.Target = target;
				menuConfig.MenuGroup = menuGroup;
				menuConfig.CreateAndFlush();
				
				return menuConfig.MenuID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("MenuConfig", string.Format("Error occured when adding a new MenuConfig, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateMenuConfig(System.Int32 menuID, System.String menuTitle, System.String menuTitle_CN, System.String menuURL, Nullables.NullableInt32 parentMenuID, Nullables.NullableInt32 menuSequenceID, System.Boolean isEnabled, System.String target, System.String menuGroup)
		{
			try
            {
                BEGDC.PreContract.DAL.MenuConfig menuConfig = GetMenuConfig(menuID);

				menuConfig.MenuTitle = menuTitle;
				menuConfig.MenuTitle_CN = menuTitle_CN;
				menuConfig.MenuURL = menuURL;
				menuConfig.ParentMenuID = parentMenuID;
				menuConfig.MenuSequenceID = menuSequenceID;
				menuConfig.IsEnabled = isEnabled;
				menuConfig.Target = target;
				menuConfig.MenuGroup = menuGroup;
				menuConfig.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("MenuConfig", string.Format("Error occured when updating the MenuConfig, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



